package com.lcj.ems.servlet;

import com.lcj.ems.entity.Emp;
import com.lcj.ems.exception.RequestParameterException;
import com.lcj.ems.exception.ServiceException;
import com.lcj.ems.service.EmpService;
import com.lcj.ems.service.impl.EmpServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("*.emp")
public class EmsServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        String servletPath = request.getServletPath();
        if ("/add.emp".contains(servletPath))
            add(request,response);
        if("/findAll.emp".contains(servletPath))
            findAll(request,response);
        if("/getEmpById.emp".equals(servletPath))
            getEmpById(request,response);
        if("/modify.emp".contains(servletPath))
            modify(request,response);
        if("/remove.emp".equals(servletPath))
            remove(request,response);

    }

    private void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EmpService empService = new EmpServiceImpl();
        String id = request.getParameter("id");
        try {
            empService.removeEmp(id);
            response.sendRedirect(request.getContextPath()+"/findAll.emp");
        } catch (RequestParameterException e) {
            e.printStackTrace();
            request.setAttribute("listEmpMsg",e.getMessage());
            request.getRequestDispatcher("empList.jsp").forward(request,response);
        } catch (ServiceException e) {
            e.printStackTrace();
            request.setAttribute("listEmpMsg",e.getMessage());
            request.getRequestDispatcher("empList.jsp").forward(request,response);
        }catch (Exception e)
        {
            e.printStackTrace();
            request.setAttribute("listEmpMsg","服务器内部异常");
            request.getRequestDispatcher("empList.jsp").forward(request,response);
        }
    }

    private void getEmpById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EmpService empService = new EmpServiceImpl();
        String id = request.getParameter("id");
        try {
            Emp empById = empService.getEmpById(id);
            request.setAttribute("emp",empById);
            request.getRequestDispatcher("modify.jsp").forward(request,response);
        } catch (RequestParameterException e) {
            e.printStackTrace();
            request.setAttribute("listEmpMsg",e.getMessage());
            request.getRequestDispatcher("empList.jsp").forward(request,response);
        } catch (ServiceException e) {
            e.printStackTrace();
            request.setAttribute("listEmpMsg",e.getMessage());
            request.getRequestDispatcher("empList.jsp").forward(request,response);
        }catch (Exception e)
        {
            e.printStackTrace();
            request.setAttribute("listEmpMsg","服务器内部异常");
            request.getRequestDispatcher("empList.jsp").forward(request,response);
        }

    }

    private void modify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EmpService empService = new EmpServiceImpl();
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String salary = request.getParameter("salary");
        String gender = request.getParameter("gender");
        String edu = request.getParameter("edu");
        String[] hobbies = request.getParameterValues("hobbies");
        try {
            empService.modify(id,name,salary,gender,edu,hobbies);
            response.sendRedirect(request.getContextPath()+"/findAll.emp");
        } catch (RequestParameterException e) {
            e.printStackTrace();
            request.setAttribute("modifyMsg",e.getMessage());
            request.getRequestDispatcher("modify.jsp").forward(request,response);
        } catch (ServiceException e) {
            e.printStackTrace();
            request.setAttribute("modifyMsg",e.getMessage());
            request.getRequestDispatcher("modify.jsp").forward(request,response);
        }catch (Exception e)
        {
            e.printStackTrace();
            request.setAttribute("modifyMsg","服务器内部异常");
            request.getRequestDispatcher("modify.jsp").forward(request,response);
        }
    }


    private void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EmpService empService = new EmpServiceImpl();

        try {
            List<Emp> all = empService.getAll();
            request.setAttribute("emplist",all);
            request.getRequestDispatcher("empList.jsp").forward(request,response);
        } catch (ServiceException e) {
            e.printStackTrace();
            request.setAttribute("listMsg",e.getMessage());
            request.getRequestDispatcher("empList.jsp").forward(request,response);
        }catch (Exception e)
        {
            e.printStackTrace();
            request.setAttribute("listMsg","服务器内部异常");
            request.getRequestDispatcher("empList.jsp").forward(request,response);
        }

    }

    private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String salary = request.getParameter("salary");
        String gender = request.getParameter("gender");
        String edu = request.getParameter("edu");
        String[] hobbies = request.getParameterValues("hobbies");
        EmpService empService = new EmpServiceImpl();
        try {
            empService.addEmp(name,salary,gender,edu,hobbies);
            response.sendRedirect(request.getContextPath()+"/findAll.emp");
        } catch (RequestParameterException e) {
            e.printStackTrace();
            request.setAttribute("addMsg",e.getMessage());
            request.getRequestDispatcher("addemp.jsp").forward(request,response);
        } catch (ServiceException e) {
            e.printStackTrace();
            request.setAttribute("addMsg",e.getMessage());
            request.getRequestDispatcher("addemp.jsp").forward(request,response);
        } catch (IOException e) {
            e.printStackTrace();
            request.setAttribute("addMsg","重定向异常");
            request.getRequestDispatcher("addemp.jsp").forward(request,response);
        }catch (Exception e)
        {
            e.printStackTrace();
            request.setAttribute("addMsg","服务器内部异常");
            request.getRequestDispatcher("addemp.jsp").forward(request,response);
        }
    }
}
